package com.mano233.server.service.serviceImpl;

import com.mano233.server.mapper.EventsMapper;
import com.mano233.server.mapper.EventsMapperCustom;
import com.mano233.server.mapper.FormMapperCustom;
import com.mano233.server.po.Events;
import com.mano233.server.po.File;
import com.mano233.server.po.Form;
import com.mano233.server.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author mano233
 */
@Service
public class EventsServiceImpl implements EventService {
    private final EventsMapper eventsMapper;
    private final EventsMapperCustom eventsMapperCustom;
    private final FormMapperCustom formMapperCustom;
    private final FormServiceImpl formService;

    @Autowired
    public EventsServiceImpl(EventsMapper eventsMapper, EventsMapperCustom eventsMapperCustom, FormMapperCustom formMapperCustom, FormServiceImpl formService) {
        this.eventsMapper = eventsMapper;
        this.eventsMapperCustom = eventsMapperCustom;
        this.formMapperCustom = formMapperCustom;
        this.formService = formService;
    }

    /**
     * 删除事件，同时删除表单下所有的表单
     *
     * @param eid 事件id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public void deleteEvent(int eid) throws Exception {
        // 1.获取事件下的所有表单
        List<Form> formList = eventsMapperCustom.getAllForm(eid);
        // 2.删除表单
        for (Form item : formList) {
            // 2.1遍历删除文件
            List<File> fileList = formMapperCustom.getFileList(item.getFormId());
            for (File fileItem : fileList) {
                formService.deleteFile(eid, fileItem.getFileId(), fileItem.getFilePath());
            }
            // 2.2删除表单
            formMapperCustom.deleteForm(item.getFormId());
        }
        // 3.最后删除事件
        eventsMapper.deleteByPrimaryKey(eid);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int addEvent(int adminId) {
        Events events = new Events();
        events.setEventPublisher(adminId);
        events.setEventAdmin(adminId);
        events.setEventCreateTime(new Date());
        events.setStatus(0);
        eventsMapper.insert(events);
        return events.getEventId();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Events updateEvent(Events newEvent, Events oldEvent) {
        int eventId = newEvent.getEventId();
        if (oldEvent.getStatus() == 0) {
            eventsMapperCustom.updateEventState(eventId, 1);
        }
        eventsMapperCustom.updateEvents(newEvent);
        return eventsMapper.selectByPrimaryKey(eventId);
    }


    @Override
    public List<Events> getAll() {
        return eventsMapper.selectAll();
    }

    /**
     * 获取管理员管理的所有事件
     * @param adminId 管理员uid
     * @return 事件集合
     */
    @Override
    public List<Events> getAdminEvents(int adminId) {
        return eventsMapperCustom.getAdminEvents(adminId);
    }

    @Override
    public Events getEventById(int id) {
        return eventsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void uploadFile(MultipartFile file) {

    }
}
